package ua.dogar.calc;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.EventQueue;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.GridLayout;

public class Calculator extends JFrame {
	private JButton btDig[];
	private JButton btOp[];
	private JLabel rezult;
	private boolean point;
	private double value;
	private char act;
	private static final int WIDTH = 330;
	private static final int HEIGHT = 380;

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Calculator frame = new Calculator();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	private Calculator() {
		//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setBounds(500, 150, WIDTH, HEIGHT);
		this.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		this.setTitle("MyCalculator");
		initalize();

	}

	private void initalize() {
		Container c = getContentPane();
		c.setLayout(new BorderLayout(15, 15));

		JPanel jPn1 = new JPanel();
		jPn1.setLayout(new GridLayout(4, 3, 10, 10));
		c.add(jPn1);

		JPanel jPn2 = new JPanel();
		jPn2.setLayout(new GridLayout(1, 1, 20, 20));
		c.add(jPn2, BorderLayout.NORTH);
		jPn2.setBackground(new Color(155, 125, 105));

		JPanel jPn3 = new JPanel();
		jPn3.setLayout(new GridLayout(6, 1, 10, 10));
		c.add(jPn3, BorderLayout.EAST);

		JPanel jPn4 = new JPanel();

		c.add(jPn4, BorderLayout.SOUTH);
		JRadioButton metal = new JRadioButton("Metal");
		metal.setActionCommand("Metal");
		metal.addActionListener(radButtEv);
		JRadioButton motif = new JRadioButton("Motif");
		motif.setActionCommand("Motif");
		motif.addActionListener(radButtEv);
		JRadioButton windows = new JRadioButton("Windows");
		windows.setActionCommand("Windows");
		windows.addActionListener(radButtEv);
		jPn4.add(metal);
		jPn4.add(motif);
		jPn4.add(windows);
		ButtonGroup bgroup = new ButtonGroup();
		bgroup.add(metal);
		bgroup.add(motif);
		bgroup.add(windows);

		rezult = new JLabel("0");

		jPn2.add(rezult);

		btDig = new JButton[11];
		for (int i = 0; i < 11; i++) {
			btDig[i] = new JButton(Integer.toString(i));
			btDig[i].addActionListener(btDigEv);

		}

		for (int i = 0; i < 11; i++) {
			jPn1.add(btDig[i]);
		}

		jPn1.add(btDig[0]);
		jPn1.add(btDig[10]);
		btDig[10].setText(",");

		btOp = new JButton[10];
		btOp[0] = new JButton("C");
		jPn3.add(btOp[0]);
		btOp[0].addActionListener(btOpEv);

		btOp[1] = new JButton("+");
		jPn3.add(btOp[1]);
		btOp[1].addActionListener(btOpEv);

		btOp[2] = new JButton("-");
		jPn3.add(btOp[2]);
		btOp[2].addActionListener(btOpEv);

		btOp[3] = new JButton("*");
		jPn3.add(btOp[3]);
		btOp[3].addActionListener(btOpEv);

		btOp[4] = new JButton("/");
		jPn3.add(btOp[4]);
		btOp[4].addActionListener(btOpEv);

		btOp[5] = new JButton("=");
		jPn3.add(btOp[5]);
		btOp[5].addActionListener(btOpEv);

	}

	private void setLookandFeel(String value) throws ClassNotFoundException,
			InstantiationException, IllegalAccessException,
			UnsupportedLookAndFeelException {
		String s = UIManager.getSystemLookAndFeelClassName();
		switch (value) {
		case "Metal":
			s = "javax.swing.plaf.metal.MetalLookAndFeel";
			break;
		case "Motif":
			s = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
			break;
		case "Windows":
			s = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
			break;
		}
		UIManager.setLookAndFeel(s);
		SwingUtilities.updateComponentTreeUI(getContentPane());
	}

	private void compute() {
		switch (act) {
		case 1:
			value += Double.parseDouble(rezult.getText());
			act = 0;
			point = false;
			break;

		case 2:
			value -= Double.parseDouble(rezult.getText());
			act = 0;
			point = false;
			break;

		case 3:
			value *= Double.parseDouble(rezult.getText());
			act = 0;
			point = false;
			break;

		case 4:
			value /= Double.parseDouble(rezult.getText());
			act = 0;
			point = false;
			break;

		default:
			value = Double.parseDouble(rezult.getText());
			act = 0;
			point = false;
			break;
		}
	}

	private ActionListener btDigEv = new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			Object x = e.getSource();
			JButton x1 = (JButton) x;
			String t1 = rezult.getText();
			String t2 = x1.getText();
			char ch = t2.charAt(0);
			if (((byte) ch >= (byte) '0') && ((byte) ch <= (byte) '9')) {

				if (t1.equals("0")) {
					rezult.setText(t2);
				} else {
					rezult.setText(t1 + t2);
				}

			} else {
				if ((point == false) && (t2.equals(","))) {
					rezult.setText(t1 + ".");
					point = true;
				}
			}
		}
	};
	private ActionListener radButtEv = new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			try {
				setLookandFeel(e.getActionCommand());
			} catch (ClassNotFoundException e1) {
				e1.printStackTrace();
			} catch (InstantiationException e1) {
				e1.printStackTrace();
			} catch (IllegalAccessException e1) {
				e1.printStackTrace();
			} catch (UnsupportedLookAndFeelException e1) {
				e1.printStackTrace();
			}
		}
	};
	private ActionListener btOpEv = new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			Object x = e.getSource();
			JButton x1 = (JButton) x;
			String text = x1.getText();

			if (text.equals("C")) {
				rezult.setText("0");
				point = false;
				value = 0;
				act = 0;
			}
			if (text.equals("+")) {
				compute();
				act = 1;
				rezult.setText("0");
				point = false;
			}

			if (text.equals("-")) {
				compute();
				act = 2;
				rezult.setText("0");
				point = false;
			}

			if (text.equals("*")) {
				compute();
				act = 3;
				rezult.setText("0");
				point = false;
			}

			if (text.equals("/")) {
				compute();
				act = 4;
				rezult.setText("0");
				point = false;
			}
			if (text.equals("=")) {
				compute();
				act = 0;
				point = false;
				rezult.setText(Double.toString(value));
			}
		}
	};
}
